﻿using System;

namespace Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL
{
    public class PickList
    {
        #region private variables

        private System.Xml.Linq.XElement m_XElement;

        private string m_ID;

        private Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Binding m_Binding;

        private System.Collections.Generic.Dictionary<string, Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.PickListItem> m_PickListItemDictionary = new System.Collections.Generic.Dictionary<string, Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.PickListItem>();

        #endregion

        public PickList(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Binding i_Binding, string i_ID)
        {
            this.PickListInitialize(i_ID);

            m_XElement = new System.Xml.Linq.XElement(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.SimpleFeedBindingsXNamespace + Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.PICKLIST_ELEMENT_NAME);

            m_XElement.SetAttributeValue(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.ID_ATTRIBUTE, m_ID);
        }

        public PickList(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Binding i_Binding, System.Xml.Linq.XElement i_PickListXElement)
        {
            if (i_Binding == null)
                throw new System.ArgumentException("i_Binding must be non-null!");

            if (i_PickListXElement == null)
                throw new System.ArgumentException("i_PickListXElement must be non-null!");

            bool InvalidElement =
                (i_PickListXElement.Name.LocalName != Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.PICKLIST_ELEMENT_NAME) ||
                (i_PickListXElement.Name.NamespaceName != Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.SIMPLE_FEED_BINDINGS_XML_NAMESPACE_URI);

            if (InvalidElement)
                throw new System.ArgumentException("Invalid i_PickListXElement!");

            m_XElement = i_PickListXElement;

            string ID = m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.ID_ATTRIBUTE).Value;

            this.PickListInitialize(ID);

            var PickListItemXElements = i_PickListXElement.Elements(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.SimpleFeedBindingsXNamespace + Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.PICKLIST_ITEM_ELEMENT_NAME);

            foreach (System.Xml.Linq.XElement PickListItemXElement in PickListItemXElements)
            {
                Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.PickListItem PickListItem = new Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.PickListItem
                    (
                    this,
                    PickListItemXElement
                    );

                this.AddPickListItem(PickListItem, false);
            }
        }

        internal void PickListInitialize(string i_ID)
        {
            if (System.String.IsNullOrEmpty(i_ID))
                throw new System.ArgumentNullException(i_ID);

            m_ID = i_ID;
        }

        public Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Binding Binding
        {
            get
            {
                return m_Binding;
            }

            internal set
            {
                if (value == null)
                    throw new System.ArgumentException("Value must be non-null!");

                m_Binding = value;
            }
        }

        public string ID
        {
            get
            {
                return m_ID;
            }
        }


        public System.Xml.Linq.XElement XElement
        {
            get
            {
                return m_XElement;
            }
        }

        public bool HasPickListItem(string i_Value)
        {
            return m_PickListItemDictionary.ContainsKey(i_Value);
        }

        public bool FindPickListItem(string i_Value, out Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.PickListItem o_PickListItem)
        {
            o_PickListItem = null;

            if (!this.HasPickListItem(i_Value))
                return false;

            o_PickListItem = m_PickListItemDictionary[i_Value];

            return true;
        }

        public Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.PickListItem GetPickListItem(string i_Value)
        {
            Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.PickListItem PickListItem = null;

            if (!this.FindPickListItem(i_Value, out PickListItem))
                throw new System.ArgumentException("Invalid value: " + i_Value);

            return PickListItem;
        }

        public System.Collections.Generic.IList<Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.PickListItem> PickListItems
        {
            get
            {
                return new System.Collections.Generic.List<Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.PickListItem>(m_PickListItemDictionary.Values);
            }
        }

        public Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.PickListItem CreatePickListItemFromXElement(System.Xml.Linq.XElement i_PickListItemElement)
        {
            Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.PickListItem PickListItem = new Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.PickListItem
                (
                this,
                i_PickListItemElement
                );

            return PickListItem;
        }

        public Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.PickListItem CreatePickListItem(string i_ID, string i_Value)
        {
            Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.PickListItem PickListItem = new Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.PickListItem
                (
                this,
                i_ID,
                i_Value
                );

            return PickListItem;
        }

        public void AddPickListItem(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.PickListItem i_PickListItem)
        {
            this.AddPickListItem
                (
                i_PickListItem,
                true
                );
        }

        private void AddPickListItem(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.PickListItem i_PickListItem, bool i_AppendXElement)
        {
            if (this.HasPickListItem(i_PickListItem.Value))
                throw new System.ArgumentException("PickListItem with same value already exists in Binding!");

            m_PickListItemDictionary.Add(i_PickListItem.Value, i_PickListItem);

            if (i_AppendXElement)
                m_XElement.Add(i_PickListItem.XElement);
        }

        public override string ToString()
        {
            return m_ID;
        }
    }
}
